<?php
/**
 * Manage content page
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class CmsModule extends CWebModule {
    public $layoutPath = '';

    public $allowPostTypes = array(
        'page' => array(
            'name' => 'Page',
            'allowComment' => false,
            'supportImage' => false
        ),
        'post' => array(
            'name' => 'Post',
            'allowComment' => true,
            'supportImage' => true
        )
    );

    public $menuIds = array('header', 'footer');

    public function init() {
        // this method is called when the module is being created
        // you may place code here to customize the module or the application
        // import the module-level models and components
        $this->setImport(array(
            'common.models.cms.*',
            'cms.models.*',
            'cms.components.*',
        ));
    }

    public function beforeControllerAction($controller, $action) {
        if (parent::beforeControllerAction($controller, $action)) {
            //check access and more


            //set default layout
            if($this->layoutPath == ''){
                $this->layoutPath = Yii::getPathOfAlias('application.views.layouts');
            }

            $this->setLayoutPath($this->layoutPath);

            return true;
        }
        else
            return false;
    }

    /**
     * check the post type is suported?
     * @param string $name name of post type
     * check query string if empty
     * @return boolean
     */
    public function isAllowPostType($name = ''){
        if($name == ''){
            if(!isset($_GET['post_type'])){
                return false;
            }

            $name = $_GET['post_type'];
        }

        foreach($this->allowPostTypes as $postType => $option){
            if($postType === $name){
                return true;
            }
        }

        return false;
    }

    public function isSupport($type = 'page', $key = 'supportImage'){
        if(isset($this->allowPostTypes[$type])){
            if(isset($this->allowPostTypes[$type][$key]) && $this->allowPostTypes[$type][$key] === true){
                return true;
            }
        }

        return false;
    }
}
